const app = getApp()
module.exports = Behavior({
  behaviors: [],
  methods: {
    formatDate (timestamp, format) {
      if (!timestamp) return
      var regYear = getRegExp("(y+)", "i")
      var time = timestamp.split('__DATE__')[1]
      if (!format) {
        format = "YYYY-mm-dd HH:MM"
      }
      time = parseInt(time);
      var realDate = getDate(time)
      function timeFormat(num) {
        return num < 10 ? '0' + num : num;
      }
      var date = [
        ["Y+", timeFormat(realDate.getFullYear())],
        ["m+", timeFormat(realDate.getMonth() + 1)],
        ["d+", timeFormat(realDate.getDate())],
        ["H+", timeFormat(realDate.getHours())],
        ["M+", timeFormat(realDate.getMinutes())],
        ["S+", timeFormat(realDate.getSeconds())]
      ]
      var reg1 = regYear.exec(format)
      if (reg1) {
        format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length))
      }
      for (var i = 0; i < date.length; i++) {
        var k = date[i][0]
        var v = date[i][1]
        var reg2 = getRegExp("(" + k + ")").exec(format)
        if (reg2) {
          format = format.replace(reg2[1], reg2[1].length == 1 ? v : ("00" + v).substring(("" + v).length))
        }
      }
      return format
    }
  }
})
